<?php
App::uses('AppController', 'Controller');

/**
 *
 */
class LoginController extends AppController {
	public $uses = array('User');
	public function beforeFilter() {
		parent::beforeFilter();
		$this -> Auth -> allow(array("index"));
	}

	/**
	 * index method
	 */
	public function index() {
		if($this -> Session -> check('Auth.User')){
			$this -> redirect("/");
		}
		$this -> layout = false;
		if ($this -> request -> is('post')) {
			if ($this -> Auth -> login()) {
				$this -> Session -> write('Auth.User.lastlogin', date('m-d-Y H:i:s'));
				$this -> redirect(
					array(
						'controller' => 'posts',
						'action' => 'index')
				);
			} else {
				$this -> Session -> setFlash(__('Invalid username or password'));
			}
		}
	}
	public function logout() {
		$uid = $this -> Session -> read('Auth.User.id');
		$this -> loadModel('User');
		$u = $this -> Session -> read('Auth.User');
		$u['lastlogin'] = date("Y-m-d H:i:s");
		$this -> User -> save($u);
		$this -> Session -> setFlash("");
		return $this -> redirect($this->Auth->logout());
	}

}
?>